/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.ProvinceDao;
import com.techshop.model.Province;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class ProvinceDaoImpl extends HibernateDaoSupport implements ProvinceDao
{
    @Override
    public Province getById(int provinceId)
    {
        try
        {
            return (Province) getSession().get(Province.class, provinceId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Province> getAll()
    {
        try
        {
            return getSession().createQuery("FROM Province").list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Province province)
    {
        try
        {
            if (getById(province.getId()) != null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().save(province);
                ValidatePhonePubSubClient.changeProvince();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean update(Province province)
    {
        try
        {
            if (getById(province.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(province));
                ValidatePhonePubSubClient.changeProvince();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Province province)
    {
        try
        {
            if (getById(province.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(province));
                ValidatePhonePubSubClient.changeProvince();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
}